package com.yupi.usercenter.feign.hystrix.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yupi.usercenter.base.model.Result;
import com.yupi.usercenter.basic.enums.HystrixEnum;
import com.yupi.usercenter.basic.vo.SqNetworkPageVO;
import com.yupi.usercenter.feign.client.SqNetworkFeignClient;
import com.yupi.usercenter.feign.hystrix.SqNetworkFeignClientHystrix;
import com.yupi.usercenter.feign.vo.SqNetworkByNameVO;
import com.yupi.usercenter.feign.vo.SqProxyAndFranchiseeNetworkVO;
import com.yupi.usercenter.feign.vo.SqProxyFinnetNetworkVO;
import com.yupi.usercenter.feign.vo.SqProxyNetworkVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 云路供应链科技有限公司 版权所有  Copyright 2020
 *
 * @author by jiweiyu
 * @since Created in 2020/10/29
 */
@Slf4j
@Component
public class SqNetworkFeignClientHystrixImpl implements SqNetworkFeignClientHystrix {

    private final SqNetworkFeignClient sqNetworkFeignClient;

    public SqNetworkFeignClientHystrixImpl(SqNetworkFeignClient sqNetworkFeignClient) {
        this.sqNetworkFeignClient = sqNetworkFeignClient;
    }

    @HystrixCommand(threadPoolKey = "lmdm-api", groupKey = "lmdm-api", commandKey = "lmdm-api.sqNetwork.page", fallbackMethod = "fallbackPage")
    @Override
    public Result<Page<SqNetworkPageVO>> page(Long current, Long size) {
        return sqNetworkFeignClient.page(current, size);
    }

    private Result<Page<SqNetworkPageVO>> fallbackPage(Long current, Long size, Throwable e) {
        log.error("fallbackPage，current:{}, size:{} error{}", current, size, e);
        return Result.error(HystrixEnum.LMDM_SERVICE_IS_ERROR);
    }

    @HystrixCommand(threadPoolKey = "sq-network-api", groupKey = "sq-network-api", commandKey = "sq-network-api.SqNetwork.getProxyNetworkByIds", fallbackMethod = "getProxyNetworkByIdsFallback")
    @Override
    public Result<List<SqProxyNetworkVO>> getProxyNetworkByIds(List<Integer> networkIds) {
        return sqNetworkFeignClient.getProxyNetworkByIds(networkIds);
    }

    public Result<List<SqProxyNetworkVO>> getProxyNetworkByIdsFallback(List<Integer> networkIds, Throwable e) {
        log.error("getProxyNetworkByIdsFallback, error ", e);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }


    @HystrixCommand(threadPoolKey = "sq-network-api", groupKey = "sq-network-api", commandKey = "sq-network-api.SqNetwork.getNetworkById", fallbackMethod = "getNetworkByIdFallback")
    @Override
    public Result<SqNetworkPageVO> getNetworkById(int id) {
        return sqNetworkFeignClient.getNetworkById(id);
    }

    public Result<SqNetworkPageVO> getNetworkByIdFallback(int id, Throwable e) {
        log.error("getNetworkByIdFallback, error ", e);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }


    @HystrixCommand(threadPoolKey = "sq-network-api", groupKey = "sq-network-api", commandKey = "sq-network-api.SqNetwork.getNetworkByNames", fallbackMethod = "getNetworkByNamesFallback")
    @Override
    public Result<List<SqNetworkByNameVO>> getNetworkByNames(List<String> networkNames) {
        return sqNetworkFeignClient.getNetworkByNames(networkNames);
    }

    public Result<List<SqNetworkByNameVO>>  getNetworkByNamesFallback(List<String> networkNames, Throwable e) {
        log.error("getNetworkByNamesFallback, error ", e);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }


    @HystrixCommand(threadPoolKey = "sq-network-api", groupKey = "sq-network-api", commandKey = "sq-network-api.SqNetwork.getAllNextDetailById", fallbackMethod = "getAllNextDetailByIdFallback")
    @Override
    public Result<List<SqNetworkPageVO>> getAllNextDetailById(int id) {
        return sqNetworkFeignClient.getAllNextDetailById(id);
    }

    public Result<List<SqNetworkPageVO>>  getAllNextDetailByIdFallback(int id, Throwable e) {
        log.error("getAllNextDetailByIdFallback, error ", e);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }


    @HystrixCommand(threadPoolKey = "sq-network-api", groupKey = "sq-network-api", commandKey = "sq-network-api.SqNetwork.getNetworkAgentInfoByIds", fallbackMethod = "getNetworkAgentInfoByIdsFallback")
    @Override
    public Result<List<SqProxyFinnetNetworkVO>> getNetworkAgentInfoByIds(List<Integer> ids) {
        return sqNetworkFeignClient.getNetworkAgentInfoByIds(ids);
    }

    public Result<List<SqProxyFinnetNetworkVO>> getNetworkAgentInfoByIdsFallback(List<Integer> ids, Throwable e) {
        log.error("getNetworkAgentInfoByIdsFallback, error ", e);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }

    @HystrixCommand(threadPoolKey = "network-api", groupKey = "network-api", commandKey = "network-api-getAllProxyNetworks", fallbackMethod = "getAllProxyNetworksFallback")
    @Override
    public Result<List<SqProxyNetworkVO>> getAllProxyNetworks() {
        return sqNetworkFeignClient.getAllProxyNetworks();
    }
    public Result<List<SqProxyNetworkVO>> getAllProxyNetworksFallback(Throwable throwable) {
        log.error("getAllProxyNetworksFallback error:", throwable);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }

    @HystrixCommand(threadPoolKey = "lmdm-api", groupKey = "lmdm-api", commandKey = "lmdm-api.sqNetwork.getProxyAndFranchiseeByIds", fallbackMethod = "fallbackGetProxyAndFranchiseeByIds")
    @Override
    public Result<Map<Integer, SqProxyAndFranchiseeNetworkVO>> getProxyAndFranchiseeByIds(List<Integer> ids) {
        return sqNetworkFeignClient.getProxyAndFranchiseeByIds(ids);
    }
    private Result<Map<Integer, SqProxyAndFranchiseeNetworkVO>> fallbackGetProxyAndFranchiseeByIds(List<Integer> ids, Throwable e) {
        log.error("fallbackGetProxyAndFranchiseeByIds，param: {} error{}", ids, e);
        return Result.error(HystrixEnum.LMDM_SERVICE_IS_ERROR);
    }

    @Override
    public Result<List<SqProxyNetworkVO>> getAllProxyNetworksNoBest() {
        return sqNetworkFeignClient.getAllProxyNetworksNoBest();
    }
    public Result<List<SqProxyNetworkVO>> getAllProxyNetworksNoBestFallback(Throwable throwable) {
        log.error("getAllProxyNetworksNoBest error:", throwable);
        return Result.error(HystrixEnum.LMDM_NETWORK_SERVICE_IS_ERROR);
    }

}
